var server = "https://www.dizhi0311.cn/api"

if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    location.href='phone.html'
} else if (/(Android)/i.test(navigator.userAgent)) {
    location.href='phone.html'
} else {
    // console.log('isPC')
}

function IEVersion() {
    if (document.documentMode) return document.documentMode;
}

var Cookie = {
    set: function (key, value, exdays) {
        //校验Key, key中不能有等号【=】
        if(key.indexOf("=") !== -1) {
            throw new Error("Cookie不支持key中使用等号【=】, key:" + key)
        }
        var exdate = new Date() // 获取时间
        exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays) // 保存的天数
        // 字符串拼接cookie
        // eslint-disable-next-line camelcase
        window.document.cookie = key + '=' + value + ';path=/;expires=' + exdate.toGMTString()
    },
    get: function (key) {
        if (document.cookie.length > 0) {
            // 这里显示的格式需要切割一下自己可输出看下
            var arr = document.cookie.split('; ')
            for (var i = 0; i < arr.length; i++) {
                var arr2 = arr[i].split('=') // 再次切割
                // 判断查找相对应的值
                if (arr2[0] === key) {
                    var value = arr2[1];
                    for (var j = 2; j < arr2.length; j++) {
                        value += '=' + arr2[j];
                    }
                    return value;
                }
            }
        }
    },
    remove: function (key) {
        this.set(key, '', -1);
    }
}

function ajax(obj,cb){
    $.ajax({
        type: obj.method,
        url: server + obj.url,
        data: obj.data,
        dateType: 'json',
        beforeSend: function(jqXHR, options){
            jqXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
            jqXHR.setRequestHeader("Authorization", 'Basic c2FiZXI6c2FiZXJfc2VjcmV0')
            jqXHR.setRequestHeader("Blade-Auth",  Cookie.get('bladeAuth'))
            jqXHR.setRequestHeader("Tenant-Id",  Cookie.get('tenantId'))
        },
        success: function (res) {
            if(res.code==401){
                layer.alert('登录超时，请重新登录')
                Cookie.remove('bladeAuth')
            } else {
                cb(res)
            }
        },
        error: function (xhr) {
            console.log(xhr)
        }
    })
}


function post(obj,cb){
    $.ajax({
        type: obj.method,
        url: server + obj.url,
        data: obj.data,
        dateType: 'json',
        beforeSend: function(jqXHR, options){
            jqXHR.setRequestHeader("Content-Type", "application/json")
            jqXHR.setRequestHeader("Authorization", 'Basic c2FiZXI6c2FiZXJfc2VjcmV0')
            jqXHR.setRequestHeader("Blade-Auth",  Cookie.get('bladeAuth'))
            jqXHR.setRequestHeader("Tenant-Id",  Cookie.get('tenantId'))
        },
        success: function (res) {
            if(res.code==401){
                layer.alert('登录超时，请重新登录')
                Cookie.remove('bladeAuth')
            } else {
                cb(res)
            }
        },
        error: function (xhr) {
            console.log(xhr)
        }
    })
}


function upload(obj,cb){
    $.ajax({
        type: obj.method,
        url: server + obj.url,
        data: obj.data,
        processData : false,
        contentType : false,
        beforeSend: function(jqXHR, options){
            jqXHR.setRequestHeader("authorization", 'Basic c2FiZXI6c2FiZXJfc2VjcmV0')
            jqXHR.setRequestHeader("Blade-Auth",  Cookie.get('bladeAuth'))
            jqXHR.setRequestHeader("Tenant-Id",  Cookie.get('tenantId'))
        },
        success: function (res) {
            cb(res)
        },
        error: function (xhr) {
            console.log(xhr)
        }
    })
}

function getUrlParams(key) {
	var url = window.location.search.substr(1)
	if (url == '') {
		return false
	}
	var paramsArr = url.split('&')
	for (var i = 0; i < paramsArr.length; i++) {
		var combina = paramsArr[i].split("=")
		if (combina[0] == key) {
			return combina[1]
		}
	}
	return false
}

function IEVersion () {
    if (document.documentMode) return document.documentMode
}



